const gulp = require('gulp'),
    sourcemaps = require('gulp-sourcemaps'),
    babel = require('gulp-babel'),
    // es2015= require('babel-preset-es2015'),  //不需要引用  但需要安装 ({presets: ['es2015']})
    // autoprefixer=require('gulp-autoprefixer'),
    rename = require('gulp-rename'),
    uglify = require('gulp-uglify'),
    // minifyHtml = require("gulp-minify-html"),
    minifyCss = require("gulp-clean-css"),
    imagemin = require('gulp-imagemin'),
    sass = require('gulp-sass'), //编译sass
    postcss=require('gulp-postcss'),  //自动添加前缀
    autoprefixer = require('autoprefixer'), //自动添加前缀
    cssnext = require('cssnext'), //自动添加前缀
    precss = require('precss'), //自动添加前缀
    watch = require('gulp-watch'),
    spritesmith = require('gulp.spritesmith'),
    pngquant = require('imagemin-pngquant'); //png图片压缩插件

    const taskES6='es6',  //es6配置
        enterES6js='src/script/*.js',
        outputES6js='src/js',

        taskMinJs='js',  //压缩js
        enterMinJs='es6/js/*.js',
        outputMinJs='js/',

        taskScss='scss',
        enterScss='src/scss/*.scss',
        outputScssMincss='src/css/';

        taskImg='img', //压缩图片
        enterImg='img/image/*',
        outputImg='img/',

        taskSprite='sprite',
        enterSprite='src/img/sprite/*.png',
        outputSprite='src/img/';


gulp.task(taskES6, function () {  //编译es6并生成.map文件
    return gulp.src(enterES6js)
        .pipe(babel({
            presets: ['es2015']
        }))
        .pipe(uglify())
        .pipe(rename({
            extname: '.min.js'
        }))
        // .pipe(sourcemaps.init())
        // .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(outputES6js));
});

gulp.task(taskScss, function () { //编译sass 压缩并输出.map文件
    return gulp.src(enterScss)
        // .pipe(gulp.dest('css/'))
        .pipe(sass())
        .pipe(postcss([autoprefixer({
            browsers: ['last 10 version', 'Firefox >= 20']
        }), cssnext, precss]))
        .pipe(minifyCss())
        .pipe(rename({
            extname: '.min.css'
        }))
        // .pipe(sourcemaps.init())
        // .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(outputScssMincss));
});

gulp.task(taskMinJs, function () {  //压缩js并生成.map文件
    return gulp.src(enterMinJs)
        // 这会输出一个未压缩过的版本
        // .pipe(gulp.dest('js/source/'))
        // 这会输出一个压缩过的并且重命名未 .min.js 的文件
        .pipe(uglify())
        .pipe(rename({
            extname: '.min.js'
        }))
        .pipe(sourcemaps.init())
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(outputMinJs));
});


// gulp.task('html', function () {
//     gulp.src('html/*.html') // 要压缩的html文件
//         .pipe(minifyHtml()) //压缩
//         .pipe(rename({
//             extname: '.min.html'
//         }))
//         .pipe(gulp.dest('html/'));
// });


gulp.task(taskImg, function () { //图片压缩
    return gulp.src(enterImg)
        .pipe(imagemin({
            progressive: true,
            use: [pngquant()] //使用pngquant来压缩png图片
        }))
        .pipe(gulp.dest(outputImg));
});




gulp.task(taskSprite, function () { //雪碧图
    return gulp.src(enterSprite)
        .pipe(spritesmith({
            // imagesName: 'sprite.png',
            imgName: 'sprite.png',
            cssName: 'sprite.css' //,
                //padding: 5,//合并时两个图片的间距
        }))
        .pipe(gulp.dest(outputSprite));
});

gulp.task('watch', function () {
    gulp.watch(enterES6js, [taskES6]);
    gulp.watch(enterScss, [taskScss]);
});